<?php
/**
 * Created by PhpStorm.
 * User: 10838
 * Date: 2017/11/1
 * Time: 14:31
 */
namespace app\admin\controller;
use app\admin\model\PrivilegeModel;
use app\common\service\ImageService;
use think\Loader;

class Privilege extends Base
{
    public function index()
    {
        $data = PrivilegeModel::all();
        $this->assign('data', $data);
        return view('index');
    }

    public function create()
    {
        return view('add');
    }

    public function store()
    {
        if (empty($_POST['title'])) {
            $this->error('权益名不能为空');
        }
        if (empty($_POST['content'])) {
            $this->error("权益内容不能为空");
        }
        $file = request()->file('file');
        if(empty($file)){
            $re = PrivilegeModel::create([
                'title' => $_POST['title'],
                'content' => $_POST['content'],
                'created_at' => time()
            ]);
        }
        if ($file) {
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads' . DS . 'privilege');
            if ($info) {
                $src = "/uploads/privilege/" . $info->getSaveName();
                $re = PrivilegeModel::create([
                    'title' => $_POST['title'],
                    'content' => $_POST['content'],
                    'src' => $src,
                    'created_at' => time()
                ]);

            } else {
                // 上传失败获取错误信息
                $this->error($file->getError());
            }


        }
        if ($re) {
            $this->success('添加成功', 'admin/privilege/index');
        } else {
            $this->error('添加失败');
        }
    }

    public function edit($id)
    {
        $data = PrivilegeModel::find($id);
        $this->assign('data', $data);
        return view('edit');
    }

    public function update()
    {
        $data = PrivilegeModel::find($_POST['id']);
        if (request()->isPost()) {
            if (empty($_POST['title'])) {
                $this->error('权益名不能为空');
            }
            if (empty($_POST['content'])) {
                $this->error("权益内容不能为空");
            }
            $file = request()->file('file');
            if ($file) {
                $is_flie = file_exists("./" . $data->src);
                if ($is_flie) {
                    unlink('./' . $data->src);
                }
                $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads' . DS . 'privilege');
                if ($info) {
                    $src = "/uploads/privilege/" . $info->getSaveName();
                    $re = PrivilegeModel::where('id', $_POST['id'])->update([
                        'title' => $_POST['title'],
                        'content' => $_POST['content'],
                        'src' => $src,
                    ]);
                }
                } else {
                    $re = PrivilegeModel::where('id', $_POST['id'])->update([
                        'title' => $_POST['title'],
                        'content' => $_POST['content']
                    ]);
                }
                if (isset($re)) {
                    $this->success('修改成功', 'admin/privilege/index');
                } else {
                    $this->error('修改失败');
                }



        } else {
            $this->error('非法操作');
        }


    }

    public function del(){
        /*$re = PrivilegeModel::find($_POST['help_id']);
        if(file_exists("./".$re->src)){
            unlink("./".$re->src);
        }*/
        $data = PrivilegeModel::destroy($_POST['help_id']);
        echo $data;

    }
}